package com.woniu.custom.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.common.dto.OrderBreakDto;
import com.woniu.common.utils.PageParam;
import com.woniu.common.utils.ResponseData;
import com.woniu.custom.dto.ChatReturnDto;
import com.woniu.custom.dto.EchartsDataDto;
import com.woniu.custom.dto.KefuDto;
import com.woniu.custom.dto.MessageDto;
import com.woniu.custom.mapper.MessageMapper;
import com.woniu.custom.pojo.Chat;
import com.woniu.custom.pojo.Message;
import com.woniu.custom.pojo.ZyCustom;
import com.woniu.custom.service.ChatService;
import com.woniu.custom.mapper.ChatMapper;
import com.woniu.custom.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author lenovo
* @description 针对表【chat(会话表)】的数据库操作Service实现
* @createDate 2024-08-13 14:56:49
*/
@Service
public class ChatServiceImpl extends ServiceImpl<ChatMapper, Chat>
    implements ChatService{
    @Autowired
    private ChatMapper chatMapper;
    @Autowired
    private MessageService messageService;

    @Override
    public ResponseData<?> queryHuiHua(PageParam<?> param) {
        PageHelper.startPage(param.getPageNum(),param.getPageSize());
        List<ChatReturnDto> chat = chatMapper.queryHuiHua(param.getParam());
        PageInfo objectPageInfo = new PageInfo<>(chat);
        return new ResponseData().success(objectPageInfo);
    }

    @Override
    public ResponseData<?> saveAll(Chat chat) {
        chatMapper.saveAll(chat);
        return new ResponseData<>().success();
    }

    @Override
    public ResponseData<?> queryChat(Long id) {
        List<Message> list = messageService.lambdaQuery().eq(Message::getChatId, id).list();
        List<MessageDto> messageDtos = new ArrayList<>();
        for (Message message : list) {
            MessageDto messageDto = new MessageDto();
            messageDto.setContent(message.getContent());
            messageDto.setType(message.getType());
            messageDto.setAvatar(null);
            messageDtos.add(messageDto);
        }

        KefuDto kefuDto = new KefuDto();
        kefuDto.setMessageDto(messageDtos);
        Chat chat = lambdaQuery().eq(Chat::getId, id).one();
        kefuDto.setUserQueryType(chat.getState());
        kefuDto.setTextarea(chat.getCreateUser());
        kefuDto.setUserPhone(chat.getUpdateUser());
        kefuDto.setUserAddr(chat.getAgentName());
        kefuDto.setUserName(chat.getClientName());

        return new ResponseData<>().success(kefuDto);
    }

    @Override
    public ResponseData<?> infoNumber() {
        Integer infoNumber = chatMapper.infoNumber();
        return new ResponseData<>().success(infoNumber);
    }

    @Override
    public List<Integer> QueryHuihuaList() {
        return chatMapper.QueryHuihuaList();
    }

    @Override
    public ResponseData<?> queryAckType() {
        List<EchartsDataDto> list = chatMapper.queryAckType();
        List<EchartsDataDto> list1 = new ArrayList<>();
        for (EchartsDataDto echartsDataDto : list) {

            if (echartsDataDto.getName().equals("3")){
                echartsDataDto.setName("投诉");
            }else if (echartsDataDto.getName().equals("6")){
                echartsDataDto.setName("咨询");
            }else {
                echartsDataDto.setName("办理");
            }
            list1.add(echartsDataDto);
        }
        return new ResponseData<>().success(list1);
    }
}




